﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NCRE学生考试端V1._0.教师端操作类
{
    public class DownloadTemplate
    {        

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
      

        #region 保存Excel模版
        /// <summary>
        /// 保存Excel模版
        /// </summary>
        /// <param name="columns">列名,例如：学号，姓名，学院</param>
        /// <param name="FileName">文件名，例如：**模版</param>
        /// <param name="SheetName">工作表名称，例如：**模版</param>
        /// <param name="message">错误信息</param>
        public static void SaveExcelTemplate(string[] columns, string FileName, string SheetName, ref string message)
        {
            string Filter = "Excel文件|*.csv|Excel 99-03文件（*.xls）|*.xls|Excel 2007文件（*.xlsx）|*.xlsx";

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.DefaultExt = "csv";//默认文件扩展名
            saveFileDialog1.FileName = FileName;//文件名
            saveFileDialog1.Filter = Filter;//文件类型
            saveFileDialog1.FilterIndex = 2;//设置默认文件类型显示顺序（可以不设置）
            saveFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
            saveFileDialog1.CreatePrompt = true;
            saveFileDialog1.Title = "Excel文件";
            saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();

            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            //获得文件路径
            string localFilePath = saveFileDialog1.FileName.ToString();
            if (Regex.IsMatch(localFilePath, @"\.csv$"))
            {
                localFilePath = Regex.Replace(saveFileDialog1.FileName, @"\.csv$", "", RegexOptions.IgnoreCase) + ".csv";
                File.WriteAllText(localFilePath, string.Join(",", columns), Encoding.Default);
            }
            else
            {
                //获取文件路径，不带文件名
                ArrayToExcelTemplate(columns, localFilePath, SheetName, ref message);
            }

            if (string.IsNullOrEmpty(message))
                MessageBox.Show("\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        #region 导出模版
        /// <summary>
        /// 导出模版
        /// </summary>
        /// <param name="columns">列名,例如：学号，姓名，学院</param>
        /// <param name="localFilePath">本地路径</param>
        /// <param name="SheetName">工作表名称，例如：**模版</param>
        /// <param name="message">错误信息</param>
        public static void ArrayToExcelTemplate(string[] columns, string localFilePath, string SheetName, ref string message)
        {
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                message = "无法创建Excel对象，可能计算机未安装Excel！";
                return;
            }

            //創建Excel對象
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            if (worksheet == null) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
            Microsoft.Office.Interop.Excel.Range range = null;

            long totalCount = columns.Length;
            worksheet.Name = SheetName;//第一个sheet在Excel中显示的名称
            int c;
            c = 0;
            ////写入标题
            for (int i = 0, count = columns.Length; i < count; i++)
            {
                worksheet.Cells[1, c + 1] = columns[i];
                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, c + 1];
                range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中  
                c++;

            }

            try
            {
                FileInfo myFile = new FileInfo(localFilePath);
                string fileExtension = Path.GetExtension(myFile.FullName);//截取扩展名
                localFilePath = Regex.Replace(localFilePath, ".xls$|.xlsx$", "", RegexOptions.IgnoreCase) + fileExtension;
                //localFilePath += xlApp.Version.CompareTo("11.0") == 0 ? ".xls" : ".xlsx";
                workbook.SaveCopyAs(localFilePath);
            }
            catch (Exception ex)
            {
                message = "生成Excel附件过程中出现异常，详细信息如：" + ex.ToString();
            }


            try
            {
                if (xlApp != null)
                {

                    int lpdwProcessId;
                    GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd), out lpdwProcessId);
                    System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
                }
            }
            catch (Exception ex)
            {
                message = "Delete Excel Process Error:" + ex.Message;
            }

        }
        #endregion

    }
}
